package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbSeckillGoods;
import com.dongdongshop.pojo.TbSeckillOrder;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.SeckillService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("seckill")
public class SeckillController {

    @DubboReference(timeout = 5000000,check = false)
    private SeckillService seckillService;

    @RequestMapping("toSeckill")
    public String toSeckill(){
        return "seckill-index";
    }
    @RequestMapping("toSeckillItem")
    public String toSeckillItem(Model model,Long id){
        TbSeckillGoods seckillGoods = seckillService.getSeckillById(id);
        model.addAttribute("seckill",seckillGoods);
        return "seckill-item";
    }
    //查询秒杀的订单信息
    @RequestMapping("getOrderId")
    @ResponseBody
    public Result getOrderId(){
        try {
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            if(user == null){
                return Result.response(ResultEnum.USER_NULL);
            }
            try {
                Long id = seckillService.selectRedisOrder(user.getUsername());
                return Result.OK().setData(id);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.response(ResultEnum.KUCUNBUZU);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @RequestMapping("getSeckill")
    @ResponseBody
    public Result getSeckills(){
        try {
            List<TbSeckillGoods> seckillList = seckillService.getSeckills();
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
    @RequestMapping("getSeckillList")
    @ResponseBody
    public Result getSeckillList(){
        try {
            List<TbSeckillGoods> seckillList = seckillService.getSeckillList();
            return Result.OK().setData(seckillList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
    @RequestMapping("addSeckill")
    @ResponseBody
    public Result addSeckill(Long id){
        try {
            Subject subject = SecurityUtils.getSubject();
            TbUser user = (TbUser) subject.getPrincipal();
            if(user == null){
                return Result.response(ResultEnum.USER_NULL);
            }
            try {
                seckillService.addSeckill(id,user);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.response(ResultEnum.KUCUNBUZU);
            }
            return Result.OK();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
}
